package m.jclecai.com.activity;


import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.dialog.SelectlistDialog;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/5/22.
 */

public class BindbankCardOldActivity extends BaseActivity {

    UserBean userBean;
    @BindView(R.id.tv_no)
    TextView tvNo;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.tv_khzh)
    TextView tvKhzh;
    @BindView(R.id.ll_show)
    LinearLayout llShow;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_khyh)
    TextView tvKhyh;
    @BindView(R.id.tv_province)
    TextView tvProvince;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.et_khh)
    EditText etKhh;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.ll_bind)
    LinearLayout llBind;

    @Override
    public int getContentView() {
        return R.layout.activity_bind_bank_old;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("绑定银行卡");
        leftOptionText("");
        userBean = PreferenceUtil.readUser(this);
    }

    @Override
    public void initData() {
        super.initData();
        userBean = PreferenceUtil.readUser(this);
        llBind.setVisibility(View.GONE);
        llShow.setVisibility(View.GONE);
        if (userBean != null && !TextUtils.isEmpty(userBean.getBankno())) {
            tvNo.setText(userBean.getBankno());
            tvBank.setText(userBean.getBank());
            tvName.setText(userBean.getRealname());
            tvArea.setText("");
            tvKhzh.setText("");
        }
        getBank();
    }

    @OnClick(R.id.iv_selest_card)
    void ivSelestCard(){
        SelectlistDialog dialog = new SelectlistDialog(this,true, SelectlistDialog.TYPE.BANK,
                tvKhyh.getText().toString(),"");
        dialog.setTitle("选择银行");
        dialog.setListener(new Callback(){
            @Override
            public void callback(Object o) {
                super.callback(o);
                tvKhyh.setText(o.toString());
            }
        });
        dialog.show();
    }

    @OnClick(R.id.iv_selest_area)
    void ivSelestArea(){
        SelectlistDialog dialog = new SelectlistDialog(this,true, SelectlistDialog.TYPE.CITY,
                "" ,"");
        dialog.setTitle("选择开户地");
        dialog.setListener(new Callback(){
            @Override
            public void callback(Object o) {
                super.callback(o);
                String s = o.toString();
                String[] split = s.split("_");
                tvProvince.setText(split[0]);
                tvCity.setText(split[1]);
            }
        });
        dialog.show();
    }

    void getBank(){
        llShow.setVisibility(View.GONE);
        llBind.setVisibility(View.GONE);
        Map<String,String> map = new HashMap<>();
        map.put("user",userBean.getUsername());
        map.put("password",PreferenceUtil.getPassword(this));
        showDialog();
        HttpManager.postAsyn(Constant_Url.GET_USER_BANK, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(BindbankCardOldActivity.this, getString(R.string.error_network));
                llShow.setVisibility(View.VISIBLE);
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                JSONObject proofBean = FastOk.get(response, JSONObject.class);
                if (proofBean != null) {
                    if (0 == (proofBean.getIntValue("status"))) {
                        tvNo.setText(proofBean.getString("bankno"));
                        tvBank.setText(proofBean.getString("bank"));
                        tvArea.append(proofBean.getString("bankprovince"));
                        tvArea.append("  ");
                        tvArea.append(proofBean.getString("bankcity"));
                        tvKhzh.setText(proofBean.getString("bankbranch"));
                        llShow.setVisibility(View.VISIBLE);
                    } else {
                        //绑定
                        llBind.setVisibility(View.VISIBLE);
                    }
                } else {
                    MyToast.showToast(BindbankCardOldActivity.this, getString(R.string.error_network));
                    llShow.setVisibility(View.VISIBLE);
                }
            }
        }, map);
    }

    //绑定身份证
    @OnClick(R.id.tv_sure)
    void bingCard() {
        //开户银行
        final String khyh = tvKhyh.getText().toString();
        //省
        final String province = tvProvince.getText().toString();
        //市
        String city = tvCity.getText().toString();
        //卡号
        final String card = etCard.getText().toString();
        //开户行
        String khh = etKhh.getText().toString();
        if (TextUtils.isEmpty(card)) {
            MyToast.showToast(this, "请输入银行卡号");
            return;
        }
        if (TextUtils.isEmpty(khh)) {
            MyToast.showToast(this, "请输入开户行名称");
            return;
        }
        showDialog();
        Map<String,String> map = new HashMap<>();
        map.put("user",userBean.getUsername());
        map.put("password",PreferenceUtil.getPassword(this));
        map.put("bank",khyh);
        map.put("bankNumber",card);
        map.put("bankno",card);
        map.put("province",province);
        map.put("city",city);
        map.put("subbranch",khh);

        HttpManager.postAsyn(Constant_Url.SET_USER_BANK, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(BindbankCardOldActivity.this, getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if (proofBean != null) {
                    MyToast.showToast(BindbankCardOldActivity.this, proofBean.getMessage());
                    if ("0".equals(proofBean.getStatus())) {
                        userBean.setBankno(card);
                        userBean.setBank(khyh);
                        PreferenceUtil.writeUser(BindbankCardOldActivity.this,userBean);
                        setResult(RESULT_OK);
                        finish();
                    }
                } else {
                    MyToast.showToast(BindbankCardOldActivity.this, getString(R.string.error_network));
                }
            }
        }, map);
    }
}
